<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/6/28 9:05
 * Modified Time: 2019/6/28 9:05
 * Version: 2.0
 */

namespace App\Modules\Service\Criterias;


use Flower\Core\Repository\Contracts\CriteriaInterface;
use Flower\Core\Repository\Contracts\RepositoryInterface;

class StoreServiceCriteria implements CriteriaInterface
{
    private $store_id;
    private $service_type;

    public function __construct($store_id,$service_type=null)
    {
        $this->store_id = $store_id;
        $this->service_type = $service_type;
    }

    /*
     * 获取本店所有服务
     */
    public function apply($model, RepositoryInterface $repository)
    {
        $store_id = $this->store_id;
        $service_type = $this->service_type;

        $select = [
            'store_service.id',
            'name',
            'price',
            'type',
            'royalty',
            'cost',
            'dictionary_item.fullname',
            'assistant_royalty'
        ];
        $model = $model->select($select)
            ->leftJoin('dictionary_item','store_service.type','dictionary_item.id')
            ->when($service_type,function ($query) use ($service_type){
                $query->where('store_service.type',$service_type);
            })
            ->where('store_service.store_id',$store_id)
            ->orderBy('store_service.created_at','DESC');

        return $model;
    }
}